# -*- coding: utf-8 -*-
import shutil
import os
import subprocess
import sys

def run_pytest():

    command = [
    sys.executable, "-m", "pytest",
    "-vs",
    # "-q",  # 安静模式
    # "--no-header",  # 不显示 pytest 版本等信息
    # "--no-summary",  # 不显示 "=== 2 passed in 0.02s ==="
    "--alluredir=./allure-results",
    "api_test_root/tests/test_login.py",
    "api_test_root/tests/test_home.py",
    "api_test_root/tests/test_site.py",
    "api_test_root/tests/test_monitor.py",
    "api_test_root/tests/test_history.py",
    "api_test_root/tests/test_alarm.py",
    "api_test_root/tests/test_report.py",
    "api_test_root/tests/test_alarm_set.py",
    "api_test_root/tests/test_sms_alarm.py",
    "api_test_root/tests/test_data_set.py",
    "api_test_root/tests/test_userlist.py",
    "api_test_root/tests/test_operation_role.py",
    "api_test_root/tests/test_data_role.py",
    "api_test_root/tests/test_log.py"
    ]   
    # 使用 subprocess 运行 pytest 命令
    result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True)
    # 输出 pytest 的执行结果
    print("=== 测试输出 ===")
    print(result.stdout)
    if result.stderr:
        print("\n=== 错误信息 ===")
        print(result.stderr)
        # ✅✅✅ 关键修复：传递 pytest 的退出码！
    print(f"\n=== pytest 退出码: {result.returncode} ===")
    sys.exit(result.returncode)  # 👈 让脚本退出码 = pytest 退出码
    #allure serve ./allure-results 
if __name__ == '__main__':
    dir_path = r"D:\Gitee\temperature_detection_api_auto\allure-results"
    if os.path.exists(dir_path):
        shutil.rmtree(dir_path)
    run_pytest()
    
